package com.tomcov.client.activities;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.tomcov.client.ClientFactory;
import com.tomcov.client.place.MapPlace;
import com.tomcov.client.place.MessageInPlace;
import com.tomcov.client.place.MessageOutPlace;
import com.tomcov.client.place.MyJourneyPlace;
import com.tomcov.client.place.NewMessagePlace;
import com.tomcov.client.place.SettingsPlace;
import com.tomcov.client.place.WelcomPlace;
import com.tomcov.client.service.UserServiceAsync;
import com.tomcov.client.view.HeaderView;

public class HeaderActivity extends AbstractActivity implements HeaderView.Presenter{

	private ClientFactory clientFactory;

	
	/**
	 * Instantiates a new header activity.
	 *
	 * @param clientFactory the client factory
	 */
	public HeaderActivity(ClientFactory clientFactory){
        this.clientFactory = clientFactory;	
	}
	
	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		HeaderView HeaderView = clientFactory.getHeaderView();
		HeaderView.getLogoutLabel().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				HeaderView HeaderView = clientFactory.getHeaderView();
				HeaderView.getLogoutLabel().setEnabled(false);
				logout();
				goTo(new WelcomPlace(null));		
				HeaderView.getLogoutLabel().setEnabled(true);
			}
		});
		HeaderView.getMapLabel().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				goTo(new MapPlace(null));		
			}
		});
		HeaderView.getMyjourneyLabel().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				goTo(new MyJourneyPlace(null));		
			}
		});
		HeaderView.getSettingsLabel().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				goTo(new SettingsPlace(null));		
			}
		});
		HeaderView.getMessageInLabel().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				goTo(new MessageInPlace(null));		
			}
		});
		HeaderView.getMessageOutLabel().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				goTo(new MessageOutPlace(null));		
			}
		});
		HeaderView.getnewMessageLabel().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				goTo(new NewMessagePlace(null));		
			}
		});
		
		HeaderView.setPresenter(this);
        panel.setWidget(HeaderView.asWidget());
	}

	@Override
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);		
	}
	
	public void logout(){
		UserServiceAsync USA=clientFactory.getUSA();
		USA.logoutUserInSession(new AsyncCallback<Integer>(){
			@Override
			public void onFailure(Throwable caught) {
				try{
						throw caught;
				}
				catch(NullPointerException e){
					
				}
				catch (Throwable e) {
					//Window.alert("logged out fail");
					e.printStackTrace();
				}
				
				
			}
			@Override
			public void onSuccess(Integer result) {
				//Window.alert("logged out");
			}

		});

	}

}
